 classdef etsData
%
% etsData 
%
% Read plant and bid data from tab-delimited text files
%
% INPUTS:
%    - Plant-level data file of plant characteristics
%    - Bid-level data file of bid prices and quantities for each period
%
% OUTPUTS:
%    - bidData object
%
properties
    
    % Properties calculated on initialization
    file_plants;              % Tab-delimited text file with plant data
    file_bids;                % Tab-delimited text file with bid data
    file_period;              % Tab-delimited text file with period data

    plant_raw;                % Raw plant data
    bid_raw;                  % Raw bid data
    period_raw;               % Raw period data
    
    plant;                    % Plant data
    bid;                      % Bid data
    period;                   % Period-level data (on emissions cap)
    
    % Switches
    noisy = 1;
    pre_covid_restrict = false;
end

methods
    
    function obj = etsData( fileplant, filebid, fileperiod, pre_covid_restrict)
        % Initiates new bidData object

        if nargin > 3
            obj.pre_covid_restrict = pre_covid_restrict;
        end
      
        % Read plant data as table object
        obj.file_plants = fileplant;
        obj.plant_raw = readtable(obj.file_plants,'Delimiter','\t',...
                        'ReadVariableNames',true);
        
        obj.file_bids   = filebid;
        obj.bid_raw = readtable(obj.file_bids,'Delimiter','\t',...
                        'ReadVariableNames',true);
                    
        obj.file_period = fileperiod;
        obj.period_raw = readtable(obj.file_period,'Delimiter','\t',...
                        'ReadVariableNames',true);
       
        obj = obj.cleanData;
        % obj.summarizeConcentrations;
                    
    end
    
    function obj = cleanData( obj )
        % Clean plant-period and plant-bid data
        
        % Plant-period variables included in simulation
        obj.plant = table( obj.plant_raw.id_plant, obj.plant_raw.period_num, ...
            obj.plant_raw.id_gpcb, 'VariableNames', ...
            {'id_plant','id_period','id_gpcb'} );
        obj.plant.treatment = obj.plant_raw.D_treatment;
        obj.plant.Hi = obj.plant_raw.heatoutput;
        obj.plant.Ait = obj.plant_raw.permit_alloc_prorated;
        
        % Emissions data
        obj.plant.Eit = obj.plant_raw.emission_val_prorated;
        obj.plant.Eit(obj.plant.Eit <= 1) = NaN;
        obj.plant.Eit( obj.plant.treatment==0 ) = ...
            obj.plant_raw.pm_mass_val_B_prorated( obj.plant.treatment==0 );
        obj.plant.ERit = obj.plant.Eit ./ obj.plant.Hi;
        obj.plant.Ebari = obj.plant_raw.pm_mass_potential_max_12;

        % Add apcd max variable. cyc<bf<scr<esp (as per stata code)
        obj.plant.apcd_max = obj.plant_raw.apcd_max;
        obj.plant.D_cyc = obj.plant_raw.apcd_present_cyclone;
        obj.plant.D_bf = obj.plant_raw.apcd_present_bagfilter;
        obj.plant.D_scr = obj.plant_raw.apcd_present_scrubber;
        obj.plant.D_esp = obj.plant_raw.apcd_present_esp;
        % Additional plant-period variables that may be useful
        % obj.plant.Ci_baseline = obj.plant_raw.emissions_conc_etsbl;
        % obj.plant.Ei_baseline = obj.plant_raw.emissions_mass_etsbl;
        % obj.plant.period_start   = datetime(obj.plant_raw.period_start);
        % obj.plant.period_end     = datetime(obj.plant_raw.period_end);
        % obj.plant.compliance_end = datetime(obj.plant_raw.compliance_end);
 
        % Calculate allocation share
        period_stats = grpstats(obj.plant,{'id_period'},...
            {'mean','numel'},'DataVars','Ait');
        period_stats.Ai_sum = period_stats.mean_Ait .* period_stats.numel_Ait;
        period_stats = period_stats(:,{'id_period','Ai_sum'});
        obj.plant = outerjoin(obj.plant,period_stats,...
                'Keys',{'id_period'},'MergeKeys',true);
        obj.plant.Ai_share = obj.plant.Ait ./ obj.plant.Ai_sum;
        
        % Sort data 
        obj.plant = sortrows(obj.plant,...
            {'id_period','id_plant'},{'ascend','ascend'});
        
        % Clean plant-bid data
        obj.bid = obj.bid_raw;
        
        % Clean period-level data on cap
        period_table = renamevars(obj.period_raw,...
            'period_cap','cap');
        period_table = sortrows(period_table,{'id_period'},{'ascend'});
        obj.period = period_table( ~isnan(period_table.id_period), : );

        if obj.pre_covid_restrict
            obj.plant = obj.plant(obj.plant.id_period < 7 ,:);
            obj.bid = obj.bid(obj.bid.id_period < 7 ,:);
            obj.period = obj.period(obj.period.id_period < 7 ,:);
        end
    end
    
    function [ ] = summarizeConcentrations( obj )
        % Stats on plant emissions for calibration of concentration standards

        % Distribution of concentration
        keep = ~isnan(obj.plant.emissions_conc_etsbl) & obj.plant.D_treatment==0;
        hist(obj.plant.emissions_conc_etsbl(keep),20);
        pHatC = lognfit(obj.plant.emissions_conc_etsbl(keep));
        display(pHatC);

        % Distribution of emissions rate
        obj.plant.emission_rate = obj.plant.emissions_mass_etsbl ./ obj.plant.heatoutput;
        keep = ~isnan(obj.plant.emission_rate) & obj.plant.D_treatment==0;
        figure; hist(obj.plant.emission_rate(keep),20);
        pHatER = lognfit(obj.plant.emission_rate(keep));
        display(pHatER);    
    end
    
    % Print homogenized bids to LaTeX tabel
%     [ ] = printHomogenizationRegression( obj, filenames )
            
end

end




